#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;

int n1, n2;
int a[N], b[N], c[N], d[N];
ll sum[N];
int d0;
int head[N];
int ver[N << 1], Next[N << 1], tot;
void add(int u, int v) {
  ++tot;
  ver[tot] = v;
  Next[tot] = head[u];
  head[u] = tot;
}
void init(int n) {
  rep(i, 1, n) head[i] = 0;
  tot = 0;
}
void dfs(int u, int fu, int mx[N], int mn[N]) {
  mx[u] = mn[u] = 0;
  for (int i = head[u]; i; i = Next[i]) {
    int v = ver[i];
    if (v == fu) continue;
    dfs(v, u, mx, mn);
    mn[u] = max(mn[u], mx[v] + 1);
    if (mn[u] > mx[u]) swap(mx[u], mn[u]);
  }
  d0 = max(d0, mx[u] + mn[u]);
}
void dfs2(int u, int fu, int mx[N], int mn[N]) {
  if (u != fu) {
    int oth = mx[fu] == mx[u] + 1 ? mn[fu] : mx[fu];
    mn[u] = max(mn[u], 1 + oth);
    if (mn[u] > mx[u]) swap(mx[u], mn[u]);
  }
  for (int i = head[u]; i; i = Next[i]) {
    int v = ver[i];
    if (v == fu) continue;
    dfs2(v, u, mx, mn);
  }
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n1;
  rep(i, 2, n1) {
    int u, v;
    cin >> u >> v;
    add(u, v), add(v, u);
  }
  dfs(1, 1, a, b);
  dfs2(1, 1, a, b);

  cin >> n2;
  init(n2);
  rep(i, 2, n2) {
    int u, v;
    cin >> u >> v;
    add(u, v), add(v, u);
  }
  dfs(1, 1, c, d);
  dfs2(1, 1, c, d);
  sort(c + 1, c + n2 + 1);
  rep(i, 1, n2) sum[i] = sum[i - 1] + c[i];
  ll ans = 0;
  rep(i, 1, n1) {
    int x = d0 - 1 - a[i];
    int mid = lower_bound(c + 1, c + n2 + 1, x) - c;
    int l = mid - 1;
    ans += 1ll * l * d0 + 1ll * (n2 - l) * (a[i] + 1) + sum[n2] - sum[l];
  }
  cout << ans;
  return 0;
}